package core

import (
	"framework-server/global"
	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
	zaprotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"go.uber.org/zap/zapcore"
	"os"
	"path"
	"time"
)

var timestampFormat = log.TimestampFormat(
	func() time.Time { return time.Now().Local() },
	"2006-01-02 15:04:05",
)

func init() {
	fileWriter, _ := zaprotatelogs.New(
		path.Join("log", "%Y-%m-%d.log"),
		zaprotatelogs.WithLinkName("latest_log"),
		zaprotatelogs.WithMaxAge(7*24*time.Hour),
		zaprotatelogs.WithRotationTime(24*time.Hour),
	)
	w := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter))
	global.GVA_LOG = log.NewLogfmtLogger(log.NewSyncWriter(w))
	global.GVA_LOG = log.With(global.GVA_LOG, "ts", timestampFormat, "caller", log.Caller(5))
}

func InitLog(debug bool) {
	if debug {
		global.GVA_LOG = level.NewFilter(global.GVA_LOG, level.AllowDebug())
	} else {
		global.GVA_LOG = level.NewFilter(global.GVA_LOG, level.AllowInfo())
	}

}
